package com.pwc.conversionTool.biz;

import java.util.List;
import java.util.Map;

import com.googlecode.objectify.Key;
import com.pwc.conversionTool.dao.AccountDao;
import com.pwc.conversionTool.dao.DataAdaptorDao;
import com.pwc.conversionTool.dao.ProjectDao;
import com.pwc.conversionTool.dao.SoapConnectionDao;
import com.pwc.conversionTool.entity.Account;
import com.pwc.conversionTool.entity.CsvDataType;
import com.pwc.conversionTool.entity.DataTemplate;
import com.pwc.conversionTool.entity.Operation;
import com.pwc.conversionTool.entity.Project;
import com.pwc.conversionTool.entity.SoapConnection;

public class ProjectBiz {
	
	
	private AccountDao accountDao=new AccountDao();
	
	private ProjectDao projectDao=new ProjectDao();
	
	private SoapConnectionDao soapConnectionDao=new SoapConnectionDao();
	
	private DataAdaptorDao dataAdaptorDao=new DataAdaptorDao();
	
	
	public void createProject(Project project,Account account)
	{
		
		Key<Account> accountKey= accountDao.saveAccount(account);
		
		project.setAccount(accountKey);
		
		projectDao.saveProject(project);
		
	}


	public Project getProjectById(Long id) {
		
		return projectDao.getProjectById(id);
		 
	}


	public List<Project> getProjectByAccount(Account currentAccount) {
		return projectDao.getProjectsByAccount(currentAccount);
		
	}


	public Operation saveOperationDetail(Long projectID,Long operationId, Long soapConnectionID, Long dataAdaptorID, String soapTemplate,String operationName) {
		
		Operation operation=null;
		
		if(operationId!=null)
		{
			operation= projectDao.getOperationById(operationId);
		}
		
		
		Project project=projectDao.getProjectById(projectID);
		
		SoapConnection soapConnection=soapConnectionDao.getSoapConnectionById(soapConnectionID);
		
		DataTemplate dataAdaptor=dataAdaptorDao.getDataAdaptorById(dataAdaptorID);		
		
		if(operation==null)
		{
			operation=new Operation(operationId,operationName,soapTemplate,soapConnection.getKey(),dataAdaptor.getKey(),project.getKey(),null);
		}else {
			operation.setOperationName(operationName);
			operation.setSoapTemplate(soapTemplate);
			operation.setSoapConnection(soapConnection.getKey());
			operation.setDataAdaptor(dataAdaptor.getKey());
			operation.setProject(project.getKey());
		}
		
		
		projectDao.saveOperation(operation);
		
		return operation;
		
	}


	public void deleteProjectById(Long id) {
		Project project= projectDao.getProjectById(id);
		
		List<Operation> operations= projectDao.findOperationByProject(project.getKey());
		
		for (Operation operation : operations) {
			projectDao.deleteOperation(operation);
		}
		
		projectDao.deleteProject(project);
		
	}


	public List<Operation> getOperationsByProject(Project project) {
		
		List<Operation> ops=projectDao.getOperationsByProject(project.getKey());
		
		for (Operation op : ops) {
			setOperationData( op);
		}
		
		return ops;
	}
	
	


	public void deleteOperationById(Long id) {
		projectDao.deleteOperationById(id);
		
	}


	public Operation getOperationById(Long operationId) {
		
		Operation operation= projectDao.getOperationById(operationId);
		
		setOperationData(operation);
		
		return operation;
	}
	
	
	private void setOperationData(Operation op)
	{
		
		//setting dataadaptor data
		DataTemplate dataAdaptor= dataAdaptorDao.getDataAdaptorById(op.getDataAdaptor().getId()) ;
		op.setDataAdaptorData(dataAdaptor);
		//setting connection
		
		SoapConnection soapConnection=soapConnectionDao.getSoapConnectionById(op.getSoapConnection().getId());
		op.setSoapConnectionData(soapConnection);
		
		
	}
	
	
	
	public Operation getOperationByKey(Key<Operation> operationKey)
	{
		Operation operation=projectDao.getOperationByKey(operationKey);
		
		return getOperationById(operation.getId());
	}
	
	



	public void saveMapping(Operation operation) {
		
		Operation oldOperation=projectDao.getOperationById(operation.getId());
		
		oldOperation.setMapping(operation.getMapping());
		
		projectDao.saveOperation(oldOperation);
		
	}

	

}
